package cn.jeefast.resthz.webservice.util;

import cn.jeefast.common.exception.RRException;
import cn.jeefast.resthz.webservice.entity.CapitalJournal;
import cn.jeefast.resthz.webservice.entity.CarFreeCarrier;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Bean2Xml
 *
 * @author WY
 * @date 2018/10/15 0015
 */
public class Bean2Xml {

  private static final Logger logger = LoggerFactory.getLogger(Bean2Xml.class);

  /************** 无车承运单 ***************/
  /**
   * 验证实体返回xml
   */
  public static String getCarMessageToXML(CarFreeCarrier c, Validator validator) {
    Set<ConstraintViolation<CarFreeCarrier>> validate = validator.validate(c);
    List<String> errorList = new ArrayList<>();
    for (ConstraintViolation<CarFreeCarrier> model : validate) {
      errorList.add(model.getMessage());
    }
    if (errorList.size() > 0) {
      logger.error(errorList.toString());
      throw new RRException(errorList.toString());
    }
    StringBuffer sb = getBean2Xml(c);

    return sb.toString();
  }

  /**
   * 通过实体返回xml
   */
  public static StringBuffer getBean2Xml(CarFreeCarrier c) {
    StringBuffer sb = new StringBuffer();

    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
    sb.append("<Root>");
    sb.append("<Header>");
    sb.append(
        "<MessageReferenceNumber>" + c.getMessageReferenceNumber() + "</MessageReferenceNumber>");
    sb.append("<DocumentName>" + c.getDocumentName() + "</DocumentName>");
    sb.append(
        "<DocumentVersionNumber>" + c.getDocumentVersionNumber() + "</DocumentVersionNumber>");
    sb.append("<SenderCode>" + c.getSenderCode() + "</SenderCode>");
    sb.append("<RecipientCode>" + c.getRecipientCode() + "</RecipientCode>");
    sb.append(
        "<MessageSendingDateTime>" + c.getMessageSendingDateTime() + "</MessageSendingDateTime>");
    sb.append("<MessageFunctionCode>" + c.getMessageFunctionCode() + "</MessageFunctionCode>");
    sb.append("</Header>");
    sb.append("<Body>");
    sb.append(
        "<OriginalDocumentNumber>" + c.getOriginalDocumentNumber() + "</OriginalDocumentNumber>");
    sb.append("<ShippingNoteNumber>" + c.getShippingNoteNumber() + "</ShippingNoteNumber>");
    sb.append("<Carrier>" + c.getCarrier() + "</Carrier>");
    sb.append("<UnifiedSocialCreditIdentifier>" + c.getUnifiedSocialCreditIdentifier()
        + "</UnifiedSocialCreditIdentifier>");
    sb.append("<PermitNumber>" + c.getCPermitNumber() + "</PermitNumber>");
    sb.append("<ConsignmentDateTime>" + c.getConsignmentDateTime() + "</ConsignmentDateTime>");
    sb.append("<BusinessTypeCode>" + c.getBusinessTypeCode() + "</BusinessTypeCode>");
    sb.append(
        "<DespatchActualDateTime>" + c.getDespatchActualDateTime() + "</DespatchActualDateTime>");
    sb.append("<GoodsReceiptDateTime>" + c.getGoodsReceiptDateTime() + "</GoodsReceiptDateTime>");
    sb.append("<ConsignorInfo>");
    sb.append("<Consignor>" + c.getConsignor() + "</Consignor>");
    sb.append("<PersonalIdentityDocument>" + c.getPersonalIdentityDocument()
        + "</PersonalIdentityDocument>");
    sb.append("<PlaceOfLoading>" + c.getPlaceOfLoading() + "</PlaceOfLoading>");
    sb.append(
        "<CountrySubdivisionCode>" + c.getCCountrySubdivisionCode() + "</CountrySubdivisionCode>");
    sb.append("</ConsignorInfo>");
    sb.append("<ConsigneeInfo>");
    sb.append("<Consignee>" + c.getConsignee() + "</Consignee>");
    sb.append("<GoodsReceiptPlace>" + c.getGoodsReceiptPlace() + "</GoodsReceiptPlace>");
    sb.append(
        "<CountrySubdivisionCode>" + c.getDCountrySubdivisionCode() + "</CountrySubdivisionCode>");
    sb.append("</ConsigneeInfo>");
    sb.append("<PriceInfo>");
    sb.append("<TotalMonetaryAmount>" + c.getTotalMonetaryAmount() + "</TotalMonetaryAmount>");
    sb.append("<Remark>" + c.getRemark() + "</Remark>");
    sb.append("</PriceInfo>");
    sb.append("<VehicleInfo>");
    sb.append("<LicensePlateTypeCode>" + c.getLicensePlateTypeCode() + "</LicensePlateTypeCode>");
    sb.append("<VehicleNumber>" + c.getVehicleNumber() + "</VehicleNumber>");
    sb.append("<VehicleClassificationCode>" + c.getVehicleClassificationCode()
        + "</VehicleClassificationCode>");
    sb.append("<VehicleTonnage>" + c.getVehicleTonnage() + "</VehicleTonnage>");
    sb.append("<RoadTransportCertificateNumber>" + c.getRoadTransportCertificateNumber()
        + "</RoadTransportCertificateNumber>");
    sb.append("<Driver>");
    sb.append("<NameOfPerson>" + c.getNameOfPerson() + "</NameOfPerson>");
    sb.append("</Driver>");
    sb.append("<TrailerVehiclePlateNumber>" + c.getTrailerVehiclePlateNumber()
        + "</TrailerVehiclePlateNumber>");
    sb.append("<Owner>" + c.getOwner() + "</Owner>");
    sb.append("<PermitNumber>" + c.getDPermitNumber() + "</PermitNumber>");
    sb.append("<QualificationCertificateNumber>" + c.getQualificationCertificateNumber()
        + "</QualificationCertificateNumber>");
    sb.append("<TelephoneNumber>" + c.getTelephoneNumber() + "</TelephoneNumber>");
    sb.append("<GoodsInfo>");
    sb.append("<DescriptionOfGoods>" + c.getDescriptionOfGoods() + "</DescriptionOfGoods>");
    sb.append("<CargoTypeClassificationCode>" + c.getCargoTypeClassificationCode()
        + "</CargoTypeClassificationCode>");
    sb.append("<GoodsItemGrossWeight>" + c.getGoodsItemGrossWeight() + "</GoodsItemGrossWeight>");
    sb.append(
        "<TotalNumberOfPackages>" + c.getTotalNumberOfPackages() + "</TotalNumberOfPackages>");
    sb.append("<Cube>" + c.getCube() + "</Cube>");
    sb.append("</GoodsInfo>");
    sb.append("</VehicleInfo>");
    sb.append("<FreeText>" + c.getFreeText() + "</FreeText>");
    sb.append("</Body>");
    sb.append("</Root>");
    return sb;
  }

  /************** 资金流水单 ***************/
  /**
   * 验证实体返回xml
   */
  public static String getCapMessageToXML(CapitalJournal c, Validator validator) {
    Set<ConstraintViolation<CapitalJournal>> validate = validator.validate(c);
    List<String> errorList = new ArrayList<>();
    for (ConstraintViolation<CapitalJournal> model : validate) {
      errorList.add(model.getMessage());
    }
    if (errorList.size() > 0) {
      logger.error(errorList.toString());
      throw new RRException(errorList.toString());
    }
    StringBuffer sb = getCap2Xml(c);

    return sb.toString();
  }

  /**
   * 通过实体返回xml
   */
  public static StringBuffer getCap2Xml(CapitalJournal c) {
    StringBuffer sb = new StringBuffer();

    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
    sb.append("<Root>");
    sb.append("<Header>");
    sb.append(
        "<MessageReferenceNumber>" + c.getMessageReferenceNumber() + "</MessageReferenceNumber>");
    sb.append("<DocumentName>" + c.getDocumentName() + "</DocumentName>");
    sb.append(
        "<DocumentVersionNumber>" + c.getDocumentVersionNumber() + "</DocumentVersionNumber>");
    sb.append("<SenderCode>" + c.getSenderCode() + "</SenderCode>");
    sb.append("<RecipientCode>" + c.getRecipientCode() + "</RecipientCode>");
    sb.append(
        "<MessageSendingDateTime>" + c.getMessageSendingDateTime() + "</MessageSendingDateTime>");
    sb.append("<MessageFunctionCode>" + c.getMessageFunctionCode() + "</MessageFunctionCode>");
    sb.append("</Header>");
    sb.append("<Body>");
    sb.append("<DocumentNumber>" + c.getDecumentNumber() + "</DocumentNumber>");
    sb.append("<Carrier>" + c.getCarrier() + "</Carrier>");
    sb.append("<VehicleNumber>" + c.getVehicleNumber() + "</VehicleNumber>");
    sb.append("<LicensePlateTypeCode>" + c.getLicensePlateTypeCode() + "</LicensePlateTypeCode>");
    sb.append("<ShippingNoteList>");
    sb.append("<ShippingNoteNumber>" + c.getShippingNoteNumber() + "</ShippingNoteNumber>");
    sb.append("<Remark>" + c.getRemark() + "</Remark>");
    sb.append("</ShippingNoteList>");
    sb.append("<Financiallist>");
    sb.append("<PaymentMeansCode>" + c.getPaymentMeansCode() + "</PaymentMeansCode>");
    sb.append("<BankCode>" + c.getBankCode() + "</BankCode>");
    sb.append("<SequenceCode>" + c.getSequenceCode() + "</SequenceCode>");
    sb.append("<MonetaryAmount>" + c.getMonetaryAmount() + "</MonetaryAmount>");
    sb.append("<DateTime>" + c.getDateTime() + "</DateTime>");
    sb.append("</Financiallist>");
    sb.append("</Body>");
    sb.append("</Root>");
    return sb;
  }
}
